<?php
require_once APP . '/application/controllers/FrontAction.php';

/**
 * 首页
 */
class IndexController extends FrontAction
{
    /**
     * 本controller通用模块
     */
    public function init ()
    {
    	// 读取广告
    	require_once APP . '/application/models/AdlistModel.php';
    	
    	$obj_ad = new AdlistModel($this->db);
    	$this->view->adlist = $obj_ad->fetchAllList();
    }
    
    /**
     * 首页
     *
     */
    public function indexAction()
    {
    	$only = $this->getParam("o");
    	// 读取文章
    	require_once APP . '/application/models/ArticleModel.php';
    	$obj_article = new ArticleModel($this->db);    	
    	
    	// 读取家族    	
    	$nowp = 1;
    	$per_page = 10;
    	
    	$jz = $this->getParam("jz");
    	if (strlen($jz) > 5){
    		$sign = substr($jz, 0, 5);
    		$nowp = (int)substr($jz, 5);
    	
	    	// 计算签名
	    	if ($sign != substr(md5($nowp . $this->cfg['slot']), 10, 5)){
	    		$nowp = 1;
	    	}
    	}
    	
    	require_once APP . '/application/models/JzlistModel.php';
    	$obj_jz = new JzlistModel($this->db);
    	$jzresult = $obj_jz->fetchIndex($nowp, $per_page);
    	
    	// 读取服务器列表
    	require_once APP . '/application/models/SfdataModel.php';
    	$obj_sf = new SfdataModel($this->db);
    	$sfresult = $obj_sf->getCurrentList();
    	
    	$this->view->article = $obj_article->fetchTop10();
    	$this->view->jzlist = $jzresult['list'];
    	$this->view->jzpage = $jzresult['page'];
    	$this->view->sf = $sfresult;
    	$this->view->only = $only;
        $this->render("index");
    }
    
    /**
     * 显示文章
     *
     */
    public function articleAction()
    {
    	$id = $this->getParam("show");
    	if (empty($id) || strlen($id) < 6)
    		$this->redirectAction("index", "index");
    	
    	$sign = substr($id, 0, 5);
    	$id = (int)substr($id, 5);
    	
    	// 计算签名
    	if ($sign != substr(md5($id . $this->cfg['slot']), 20, 5)){
    		$this->redirectAction("index", "index");
    	}
    	
    	require_once APP . '/application/models/ArticleModel.php';
    	$obj_article = new ArticleModel($this->db);
    	$this->view->articleinfo = $obj_article->show($id);
    	
    	$this->render("article");
    }
    
    /**
     * 发布家族
     *
     */
    public function jzpubAction()
    {
    	$this->render("jzpub");
    }
    
    /**
     * 添加家族
     *
     */
    public function dojzpubAction()
    {
    	$param = $this->getAllParams();
    	foreach ($param as $key => $v){
    		$param[$key] = trim($v);
    	}
    	$nparam = array();
    	$nparam['name'] = mb_substr($param['name'], 0, 10);
    	$nparam['qqqun'] = mb_substr($param['qqqun'], 0, 10);
    	$nparam['master'] = mb_substr($param['master'], 0, 10);
    	$nparam['version'] = mb_substr($param['version'], 0, 20);
    	$nparam['url'] = mb_substr($param['url'], 0, 50);
    	$nparam['intro'] = mb_substr($param['intro'], 0, 10);
    	
    	if (empty($nparam['name'])){
    		$this->showmessage("家族名称不能为空！");
    	}
    	
    	$r = preg_match("/^\d{0,10}$/", $nparam['qqqun'], $matchs);
    	if ( $r == 0)
    		$this->showmessage("QQ群格式错误！");
    	    		
    	require_once APP . '/application/models/JzlistModel.php';
    	$obj_jz = new JzlistModel($this->db);
    	$obj_jz->add($nparam);
    	
    	$result = array("status" => 1, "message" => "添加成功！请等待审核！");
    	
    	echo json_encode($result);
    }
    
    /**
     * 发布广告
     *
     */
    public function doapubAction()
    {
    	$param = $this->getAllParams();
    	foreach ($param as $key => $v){
    		$param[$key] = trim($v);
    	}
    	
    	if ($param['sdatetime_minute'] != '00' && $param['sdatetime_minute'] != '30')
    		$param['sdatetime_minute'] = '00';
    	
    	$nparam = array();
    	$nparam['name'] = mb_substr($param['name'], 0, 8);
    	$nparam['ip'] = mb_substr($param['ip'], 0, 8);
    	$nparam['sdatetime'] = $param['sdatetime_date'] . ' ' . $param['sdatetime_hour'] . ':' . $param['sdatetime_minute'];
    	$nparam['routeline'] = mb_substr($param['routeline'], 0, 6);
    	$nparam['intro'] = mb_substr($param['intro'], 0, 18);
    	$nparam['qq'] = mb_substr($param['qq'], 0, 10);
    	$nparam['url'] = mb_substr($param['url'], 0, 50);
    	$nparam['pubtime'] = time();
    	$nparam['pubip'] = $this->getIp(true);
    	
    	if (empty($nparam['name'])){
    		$this->showmessage("名称不能为空！");
    	}
    	    	    		
    	require_once APP . '/application/models/SfdataModel.php';
    	$obj_sf = new SfdataModel($this->db);
    	$obj_sf->add($nparam);
    	
    	$result = array("status" => 1, "message" => "添加成功！请等待审核！");
    	
    	echo json_encode($result);
    }
    
    public function adpubAction()
    {
    	$this->render("adpub");
    }
    
    /**
     * 发布广告
     *
     */
    public function pubaAction()
    {
    	$this->render("puba");
    }
    
    public function contactAction()
    {
    	$this->render("contact");
    }
    
    public function downAction()
    {
    	$this->render("down");
    }
}
